#!/usr/bin/env python
#
# pre_processor.py is part of pyStrips
# Copyright (C) - 2009 - Juan B. Cabral <jbcabral_89@yahoo.com.ar>

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


################################################################################
# DOCS
################################################################################

'''Transform a strips source code in list and dict structures'''


################################################################################
# IMPORTS
################################################################################

import re

import yaml

import common
import error

################################################################################
# PRIVATE FUNCTIONS
################################################################################

def _validate_chars(src):
    for n, line in enumerate(src.splitlines()):
        if common.RW_COMMENT in line:
            idx = line.index(common.RW_COMMENT)
            line = line[0:idx]
        if common.RE_INVALID_CHARS.search(line):
            msg = "Invalid statement '%s'" % line.strip()
            raise error.PyStripsError(msg, error.SYNTAX_ERROR, line)


def _prepare_src(src):
    return src.replace(common.RW_PREDICATES + common.RW_BLOCK_START,
                      common.RW_PREDICATES + common.RW_BLOCK_START + " |")


def _format_predicates(yml_src):
    preds = str(yml_src[common.RW_PREDICATES])
    yml_src[common.RW_PREDICATES] = [p for p in preds.splitlines() if p.strip()]
   
     
def _format_and(yml_src):
    for name, block  in yml_src.items():
        if name.startswith(common.RW_STATE):
                preds = str(block)
                yml_src[name] = [p for p in preds.split(common.RW_AND) if p.strip()]
        elif name.startswith(common.RW_OPERATOR):
            for op_block_name, op_block in block.items():
                preds = str(op_block)
                block[op_block_name] = [p for p in preds.split(common.RW_AND) if p.strip()]        
    

################################################################################
# PUBLIC FUNCTIONS
################################################################################

def pre_process(src):
    '''Transform a strips source code in list and dict structures'''
    try:
        _validate_chars(src)
        src = _prepare_src(src)
        yml_src = yaml.load(src)
        _format_predicates(yml_src)
        _format_and(yml_src)
        return yml_src
    except yaml.scanner.ScannerError, err:
        raise error.PyStripsError(str(err), error.SYNTAX_ERROR, "")


################################################################################
# MAIN
################################################################################
    
if __name__ == '__main__':
    print(__doc__)
